using FytSoa.Domain.Car;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using FytSoa.Domain.User;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Car;

/// <summary>
/// 服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v3")]
public class CarsPrizeService : IApplicationService 
{
    private readonly SugarRepository<CarsPrize> _thisRepository;
    public CarsPrizeService(SugarRepository<CarsPrize> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<CarsPrizeDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Name.Contains(param.Key))
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<CarsPrizeDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CarsPrizeDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<CarsPrizeDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<long> AddAsync(CarsPrizeDto model)
    {
        model.Id = Unique.Id();
        await _thisRepository.InsertAsync(model.Adapt<CarsPrize>());
        return model.Id;
    }

    /// <summary>
    /// 为用户添加抽奖次数
    /// </summary>
    /// <returns></returns>
    public async Task AddDrawAsync(CarsPrizeDrawParam model)
    {
        var prizeModel = await _thisRepository.GetByIdAsync(model.Id);
        prizeModel.DrawNumber = model.Users;
        await _thisRepository.UpdateAsync(prizeModel);
        //增加用户抽奖次数
        var userRepository = _thisRepository.ChangeRepository<SugarRepository<Member>>();
        var userList = await userRepository.GetListAsync(m => model.Users.Select(s => s.Id).Contains(m.Id));
        foreach (var item in userList)
        {
            var draw = model.Users.FirstOrDefault(m => m.Id == item.Id);
            if (draw != null) item.PrizeDrawNum += draw.Number;
        }

        if (userList.Count>0)
        {
            await userRepository.UpdateRangeAsync(userList);
        }
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CarsPrizeDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CarsPrize>());

    /// <summary>
    /// 删除,支持多个
    /// </summary>
    /// <param name="ids">逗号分隔</param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync(string ids) =>
        await _thisRepository.DeleteAsync(m => ids.StrToListLong().Contains(m.Id));

    /// <summary>
    /// 删除-内定用户
    /// </summary>
    /// <returns></returns>
    [HttpPost("del")]
    public async Task<bool> DeleteInternalAsync(CarsPrizeDelInternalParam param)
    {
        var model = await _thisRepository.GetByIdAsync(param.PrizeId);
        foreach (var item in model.Prizes)
        {
            if (item.Name==param.Info.Name && item.Code==param.Info.Code)
            {
                item.Users.Remove(param.UserId.ToString());
            }
        }
        return await _thisRepository.UpdateAsync(model);
    }
}
